package com.jiayi.weiphoto.activity;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ListView.FixedViewInfo;

import com.jiayi.weiphoto.R;
import com.jiayi.weiphoto.entity.YPentity;
import com.jiayi.weiphoto.page.FixedHeightImageView;
import com.jiayi.weiphoto.page.FixedWidthImageView;
import com.jiayi.weiphoto.page.YPSmartImageView;
import com.jiayi.weiphoto.util.MyLog;
import com.jiayi.weiphoto.util.SearchSyncImageLoader;
import com.jiayi.weiphoto.util.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class YPListAdapter extends BaseAdapter{
	List<YPentity> yp=null;
	Context context=null;
	View parentView;
	SearchSyncImageLoader imageLoader;

	public YPListAdapter(Context context, List<YPentity> yp, ListView parentView) {
		this.yp=yp;
		this.context=context;
		this.parentView = parentView;
		imageLoader = SearchSyncImageLoader.getInstance();
		imageLoader.setCacheRootPath(StringUtil.genCompanyRootPath(context));
		
	}
	
	@Override
	public int getCount() {
		return yp.size();
	}
	
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return yp.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		Holder holder = null;
		
		if (convertView == null) {
			holder = new Holder();
			view = LayoutInflater.from(context).inflate(R.layout.yp_item, null);
			holder.iv_yp_item=(FixedWidthImageView ) view.findViewById(R.id.iv_yp_item);
			view.setTag(holder);
		} 
		else {
			holder = (Holder) view.getTag();
		}
		
		String url = MainConstants.HOST_img + yp.get(position).getPicture();
		holder.iv_yp_item.setTag(url);
		if(yp.get(position).getCoverBitmap() == null){
			imageLoader.loadImage(position, url, new SearchSyncImageLoader.OnImageLoadListener() {
				
				@Override
				public void onImageLoad(String iconUrl, Bitmap bitmap, int pos) {
					// TODO Auto-generated method stub
					FixedWidthImageView imgView = (FixedWidthImageView) parentView.findViewWithTag(iconUrl);
					if(imgView != null){
						imgView.setImageBitmap(bitmap);
//						imgView.setImageResource(R.drawable.wel);
					}
					MyLog.e("onImageLoad      "+iconUrl);
					yp.get(pos).setCoverBitmap(bitmap);
				}
				
				@Override
				public void onError(Integer t) {
					// TODO Auto-generated method stub
					
				}
			});
		}else{
			holder.iv_yp_item.setImageBitmap(yp.get(position).getCoverBitmap());
		}
		return view;
	}
	
	static class Holder {
		FixedWidthImageView iv_yp_item;
	}
}
